<template>
  <div class="workflow-display">
    <div class="workflow-header">
      <div class="header-title">
        <el-icon class="header-icon">
          <Connection />
        </el-icon>
        <h3>工作流设计</h3>
      </div>
    </div>

    <el-container style="height: 1000px;">
      <el-main>
        <div style="width: 100%;height: 900px;">
          <VueFlowDisplay :data="workflow" @currentNodeInfo="handleNodeInfo" @currentEdgeInfo="handleEdgeInfo">
          </VueFlowDisplay>
        </div>
      </el-main>
      <el-aside style="border:1px solid #f3f3f3; ;padding: 0;" v-if="historyStore.currentCode === ''">
        <el-tabs type=" border-card" class="demo-tabs" style="height: 1000px;" v-model="activeTab">
          <el-tab-pane name="nodeTab" label="节点">

            <div>
              <div class="form-group">
                <label>节点名称</label>
                <input type="text" v-model="currentNode.name" placeholder="节点名称">
              </div>
              <div class="form-group">
                <label>节点描述</label>
                <textarea v-model="currentNode.description" placeholder="节点详细描述"></textarea>
              </div>
              <div class="btn-group">
                <button class="btn btn-primary" @click="editNode">
                  更改
                </button>
                <button class="btn btn-primary" @click="removeNode">
                  移除
                </button>
              </div>

            </div>
            <el-divider />
            <div>
              <div class="form-group">
                <label>节点名称</label>
                <input type="text" v-model="newNode.name" placeholder="输入节点名称">
              </div>
              <div class="form-group">
                <label>节点描述</label>
                <textarea v-model="newNode.description" placeholder="输入节点详细描述"></textarea>
              </div>
              <div class="btn-group">
                <button class="btn btn-primary" @click="addNode">
                  添加节点
                </button>
              </div>
            </div>
          </el-tab-pane>

          <el-tab-pane label="连边" name="edgeTab">
            <div>
              <div class="form-group">
                <label>源节点</label>
                <input readonly="true" type="text" v-model="currentEdge.sourceName" placeholder="(未选中)">
              </div>
              <div class="form-group">
                <label>目标节点</label>
                <input readonly="true" type="text" v-model="currentEdge.targetName" placeholder="(未选中)">
              </div>
              <div class="form-group">
                <label>跳转条件(正向)</label>
                <textarea v-model="currentEdge.left" placeholder="(必填)"></textarea>
              </div>
              <div class="form-group">
                <label>跳转条件(返向)</label>
                <textarea v-model="currentEdge.right" placeholder="(不输入则为单向边）"></textarea>
              </div>
              <div class="btn-group">
                <button class="btn btn-primary" @click="editEdge">
                  更改
                </button>
                <button class="btn btn-primary" @click="removeEdge">
                  移除
                </button>
              </div>
            </div>
            <el-divider />
            <div>
              <div class="form-group">
                <label>源节点</label>
                <el-select v-model="newEdge.source" placeholder="选择源节点">
                  <el-option v-for="node in props.workflow.workflow.dataflows" :key="node.id" :label="node.name"
                    :value="node.id" />
                </el-select>

              </div>
              <div class="form-group">
                <label>目标节点</label>
                <el-select v-model="newEdge.target" placeholder="选择目标节点">
                  <el-option v-for="node in props.workflow.workflow.dataflows" :key="node.id" :label="node.name"
                    :value="node.id" />
                </el-select>
              </div>
              <div class="form-group">
                <label>跳转条件(正向)</label>
                <textarea v-model="newEdge.left" placeholder="(必填)"></textarea>
              </div>
              <div class="form-group">
                <label>跳转条件(返向)</label>
                <textarea v-model="newEdge.right" placeholder="(不输入则为单向边）"></textarea>
              </div>
              <div class="btn-group">
                <button class="btn btn-primary" @click="addEdge">
                  添加连边
                </button>
              </div>

            </div>
          </el-tab-pane>
        </el-tabs>
      </el-aside>
    </el-container>
  </div>
</template>

<script setup>
import { ref, computed, watch, reactive } from 'vue';
import VueFlowDisplay from './VueFlowDisplay.vue';
import {
  Connection,

} from '@element-plus/icons-vue';
import { useHistoryStore } from '@/stores/history';

const historyStore = useHistoryStore();
// 接收的属性
const props = defineProps({
  workflow: {
    type: [Object, String],
    default: () => ({})
  },
});
console.log("props.workflow", props.workflow)
const activeTab = ref('nodeTab')


const newNode = reactive({
  name: '',
  description: ''
});
const newEdge = reactive({
  source: '',
  target: '',
  left: '',
  right: '',
});
const currentNode = reactive({
  id: '',
  name: '',
  description: ''
});
const currentEdge = reactive({
  id: '',
  source: '',
  sourceName: '',
  target: '',
  targetName: '',
  left: '',
  right: '',
});
function clearCurrentNode() {
  currentNode.id = ''
  currentNode.description = ''
  currentNode.name = ''
}
function clearCurrentEdge() {
  currentEdge.id = ''
  currentEdge.source = ''
  currentEdge.target = ''
  currentEdge.sourceName = ''
  currentEdge.targetName = ''
  currentEdge.left = ''
  currentEdge.right = ''
}
function clearNewNode() {
  newNode.name = ''
  newNode.description = ''
}
function clearNewEdge() {
  newEdge.source = ''
  newEdge.target = ''
  newEdge.left = ''
  newEdge.right = ''
}
function updateWorkflow() {
  console.log("原来的：", historyStore.currentWorkflow.workflow.dataflows)
  console.log("新的：", props.workflow.workflow.dataflows)
  historyStore.currentWorkflow.workflow.dataflows = props.workflow.workflow.dataflows;
}
function addNode() {
  if (!newNode.name || !newNode.description) {
    alert('请填写完整的节点信息');
    return;
  }
  props.workflow.workflow.dataflows.push({
    id: props.workflow.workflow.dataflows[props.workflow.workflow.dataflows.length - 1].id + 1 || 1, // 自动生成ID
    name: newNode.name,
    description: newNode.description,
    input: "",
    output: '',
    "jump-list": [],
  });
  clearNewNode();
  updateWorkflow()
}
function editNode() {
  if (!currentNode.id) {
    alert('请先选择一个节点进行编辑');
    return;
  }
  const node = props.workflow.workflow.dataflows.find(n => n.id === currentNode.id);
  if (node) {
    node.name = currentNode.name;
    node.description = currentNode.description;
  } else {
    alert('未找到该节点');
  }
  updateWorkflow()
}
function removeNode() {
  if (!currentNode.id) {
    alert('请先选择一个节点进行删除');
    return;
  }
  const index = props.workflow.workflow.dataflows.findIndex(n => n.id === currentNode.id);
  if (index !== -1) {
    props.workflow.workflow.dataflows.splice(index, 1);
    props.workflow.workflow.dataflows.forEach(node => {
      if (node["jump-list"] && node["jump-list"].length > 0) {
        node["jump-list"] = node["jump-list"].filter(jump => jump.id !== currentNode.id);
      }
    });
    clearCurrentNode();
  } else {
    alert('未找到该节点');
  }
  updateWorkflow()
}
function addLeftEdge(edge) {
  const sourceNode = props.workflow.workflow.dataflows.find(n => n.id === edge.source);
  const existingEdge = sourceNode["jump-list"].find(jump => jump.id === edge.target);
  if (existingEdge) {
    alert('该连边已存在');
    return;
  } else {
    sourceNode["jump-list"].push({
      id: edge.target,
      condition: edge.left,
    });
  }
}
function addRightEdge(edge) {
  const targetNode = props.workflow.workflow.dataflows.find(n => n.id === edge.target);
  const existingEdge = targetNode["jump-list"].find(jump => jump.id === edge.source);
  if (existingEdge) {
    alert('该连边已存在');
    return;
  } else {
    targetNode["jump-list"].push({
      id: edge.source,
      condition: edge.right,
    });
  }
}
function addEdge() {
  if (!newEdge.source || !newEdge.target || !newEdge.left) {
    alert('请填写完整的连边信息');
    return;
  } else {
    if (newEdge.left != '') {
      addLeftEdge(newEdge);
    }
    if (newEdge.right != '') {
      addRightEdge(newEdge);
    }
  }

  clearNewEdge();
  updateWorkflow()
}
function removeLeftEdge() {
  if (!currentEdge.id) {
    alert('请先选择一个连边进行删除');
    return;
  }
  const sourceNode = props.workflow.workflow.dataflows.find(n => n.id === currentEdge.source);
  const index = sourceNode["jump-list"].findIndex(jump => jump.id === currentEdge.target);
  if (index !== -1) {
    sourceNode["jump-list"].splice(index, 1);
  }
}
function removeRightEdge() {
  if (!currentEdge.id) {
    alert('请先选择一个连边进行删除');
    return;
  }
  const targetNode = props.workflow.workflow.dataflows.find(n => n.id === currentEdge.target);
  const index = targetNode["jump-list"].findIndex(jump => jump.id === currentEdge.source);
  if (index !== -1) {
    targetNode["jump-list"].splice(index, 1);
  }
}
function removeEdge() {
  if (!currentEdge.id) {
    alert('请先选择一个连边进行删除');
    return;
  }
  removeLeftEdge();
  removeRightEdge();
  updateWorkflow()

}
function editEdge() {
  if (!currentEdge.id) {
    alert('请先选择一个连边进行编辑');
    return;
  }
  if (currentEdge.left == '') {
    removeLeftEdge();
  }
  if (currentEdge.right == '') {
    removeRightEdge();
  }
  if (currentEdge.left != '') {
    const sourceNode = props.workflow.workflow.dataflows.find(n => n.id === currentEdge.source);
    const index = sourceNode["jump-list"].findIndex(jump => jump.id === currentEdge.target);
    if (index !== -1) {
      sourceNode["jump-list"][index].condition = currentEdge.left;
    } else {
      addLeftEdge(currentEdge);
    }
  }
  if (currentEdge.right != '') {
    const targetNode = props.workflow.workflow.dataflows.find(n => n.id === currentEdge.target);
    const index = targetNode["jump-list"].findIndex(jump => jump.id === currentEdge.source);
    if (index !== -1) {
      targetNode["jump-list"][index].condition = currentEdge.right;
    } else {
      addRightEdge(currentEdge);
    }
  }
  updateWorkflow()

}
function handleNodeInfo(nodeInfo) {
  currentNode.id = nodeInfo.id
  currentNode.description = nodeInfo.description
  currentNode.name = nodeInfo.name
  activeTab.value = 'nodeTab'
  clearCurrentEdge()
}
function handleEdgeInfo(edgeInfo) {
  currentEdge.id = `${edgeInfo.source}->${edgeInfo.target}`
  currentEdge.source = edgeInfo.source
  currentEdge.target = edgeInfo.target
  currentEdge.sourceName = edgeInfo.sourceName
  currentEdge.targetName = edgeInfo.targetName
  currentEdge.left = edgeInfo.left
  currentEdge.right = edgeInfo.right
  activeTab.value = 'edgeTab'
  clearCurrentNode()
}
watch(activeTab, () => {
  if (activeTab.value == 'nodeTab') {
    clearCurrentEdge()
  } else {
    clearCurrentNode()
  }
})














</script>

<style>
.workflow-dialog .el-dialog__header {
  padding: 16px 20px;
  margin-right: 0;
  border-bottom: 1px solid #ebeef5;
}

.workflow-dialog .el-dialog__body {
  padding: 0;
}

.workflow-dialog .el-tabs__header {
  margin: 0;
}
</style>

<style scoped>
.demo-tabs>.el-tabs__content {
  padding: 32px;
  color: #6b778c;
  font-size: 32px;
  font-weight: 600;
}

.demo-tabs .custom-tabs-label .el-icon {
  vertical-align: middle;

}

.demo-tabs .custom-tabs-label span {
  vertical-align: middle;
  margin-left: 4px;
}

.workflow-display {
  margin: 20px 0;
  padding: 0;
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  transition: box-shadow 0.3s ease;
}

.workflow-display:hover {
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
}

.workflow-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  background-color: #f8fafc;
  border-bottom: 1px solid #ebeef5;
}

.header-title {
  display: flex;
  align-items: center;
}

.header-icon {
  font-size: 22px;
  margin-right: 10px;
  color: #409eff;
}

.workflow-header h3 {
  margin: 0;
  font-size: 18px;
  color: #303133;
  font-weight: 600;
}

.actions {
  display: flex;
  gap: 10px;
}

.action-button {
  display: flex;
  align-items: center;
  gap: 6px;
}

.workflow-content {
  padding: 0;
}

.json-display {
  padding: 16px 20px;
}

.json-actions {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 12px;
}

.copy-button {
  display: flex;
  align-items: center;
  gap: 6px;
}

.json-code {
  margin: 0;
  padding: 16px;
  background-color: #f8fafc;
  border-radius: 8px;
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, Courier, monospace;
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 500px;
  overflow: auto;
  border: 1px solid #ebeef5;
}

.flow-display {
  display: flex;
  flex-direction: column;
  padding: 24px;
}

.flow-container {
  min-height: 300px;
  border: 1px dashed #dcdfe6;
  border-radius: 8px;
  margin: 20px 0;
  display: flex;
  justify-content: center;
  align-items: center;
  color: #909399;
}

.flow-container.fullscreen {
  min-height: 500px;
}

.json-data {
  margin-top: 20px;
}

.note {
  color: #909399;
  font-size: 14px;
  margin-top: 8px;
}

.fullscreen-workflow {
  height: 70vh;
  display: flex;
  flex-direction: column;
}

/* 组件卡片样式 */
.components-info {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 16px;
  padding: 20px;
}

.component-card {
  border: 1px solid #ebeef5;
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.2s, box-shadow 0.2s;
}

.component-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.component-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  background-color: #f8fafc;
  border-bottom: 1px solid #ebeef5;
}

.component-id {
  font-weight: 600;
  font-size: 16px;
}

.component-body {
  padding: 16px;
}

.component-detail {
  margin-bottom: 12px;
}

.component-detail:last-child {
  margin-bottom: 0;
}

.dependency-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}

.dep-tag {
  margin-right: 0 !important;
}

.module-list {
  margin-top: 8px;
  margin-bottom: 0;
  padding-left: 20px;
}

.module-components {
  color: #909399;
  font-size: 13px;
}

.el-aside {
  overflow: hidden;
}

.sidebar {
  width: 320px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  padding: 25px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  display: flex;
  flex-direction: column;
  gap: 25px;
  overflow-y: auto;
}

.sidebar-section {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
  transition: transform 0.3s ease;
}

.sidebar-section:hover {
  transform: translateY(-5px);
}

.section-title {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
  padding-bottom: 15px;
  border-bottom: 2px solid #f0f0f0;
  color: #2c3e50;
  font-size: 1.3rem;
}

.section-title i {
  color: #3498db;
  font-size: 1.5rem;
}

.form-group {
  margin-bottom: 12px;
}

label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
  color: #555;
}

input,
select,
textarea {
  width: 100%;
  padding: 6px 15px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  font-size: 1rem;
  transition: border 0.3s;
}

input:focus,
select:focus,
textarea:focus {
  border-color: #3498db;
  outline: none;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.2);
}

textarea {
  min-height: 60px;
  resize: vertical;
}

.btn-group {
  display: flex;
  gap: 12px;
  margin-top: 8px;
}

.btn {
  flex: 1;
  padding: 8px 15px;
  border: none;
  border-radius: 8px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.btn-primary {
  background: #3498db;
  color: white;
}

.btn-primary:hover {
  background: #2980b9;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(52, 152, 219, 0.4);
}

.btn-danger {
  background: #e74c3c;
  color: white;
}

.btn-danger:hover {
  background: #c0392b;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(231, 76, 60, 0.4);
}

.btn-success {
  background: #2ecc71;
  color: white;
}

.btn-success:hover {
  background: #27ae60;
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(46, 204, 113, 0.4);
}

.flow-container {
  flex: 1;
  position: relative;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 15px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  overflow: hidden;
}

.vue-flow {
  width: 100%;
  height: 100%;
  background: #f8f9fa;
}

.tooltip {
  position: absolute;
  background: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 15px;
  box-shadow: 0 5px 20px rgba(0, 0, 0, 0.15);
  z-index: 100;
  max-width: 320px;
  pointer-events: none;
  transform: translate(15px, 15px);
}

.tooltip-header {
  font-weight: bold;
  margin-bottom: 10px;
  padding-bottom: 8px;
  border-bottom: 1px solid #eee;
  color: #2c3e50;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  gap: 8px;
}

.tooltip-content p {
  margin: 8px 0;
  line-height: 1.5;
  color: #555;
}

.status-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(255, 255, 255, 0.9);
  padding: 12px 20px;
  display: flex;
  justify-content: space-between;
  font-size: 0.9rem;
  color: #555;
  border-top: 1px solid #eee;
  z-index: 10;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px;
  color: #777;
}

.empty-state i {
  font-size: 3rem;
  margin-bottom: 20px;
  color: #ccc;
}

.selected-item {
  background: #e3f2fd;
  border-left: 4px solid #3498db;
  padding: 15px;
  border-radius: 8px;
  margin-top: 15px;
}

.selected-item h4 {
  margin-bottom: 8px;
  color: #2c3e50;
  display: flex;
  align-items: center;
  gap: 8px;
}

.info-row {
  display: flex;
  gap: 15px;
  margin-top: 10px;
}

.info-item {
  flex: 1;
  background: white;
  padding: 12px;
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
}

.info-label {
  font-size: 0.85rem;
  color: #777;
  margin-bottom: 5px;
}

.info-value {
  font-weight: 600;
  color: #333;
}

.custom-node {
  background: white;
  border: 2px solid #3498db;
  border-radius: 8px;
  padding: 15px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
  min-width: 200px;
  text-align: center;
  transition: all 0.3s ease;
}

.custom-node:hover {
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.15);
  transform: translateY(-3px);
}

.node-header {
  background: #3498db;
  color: white;
  padding: 8px 15px;
  border-radius: 6px;
  margin: -15px -15px 15px -15px;
  font-weight: 600;
}

.node-content {
  color: #555;
  font-size: 0.95rem;
}

.custom-edge {
  stroke: #95a5a6;
  stroke-width: 2;
  stroke-dasharray: 5;
}

.custom-edge.selected {
  stroke: #e74c3c;
  stroke-width: 3;
}

.edge-label {
  fill: #333;
  font-size: 0.9rem;
  background: rgba(255, 255, 255, 0.9);
  padding: 3px 8px;
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}
</style>